import {
  createRouter,
  createWebHistory,
  type RouteRecordRaw,
} from "vue-router";
import HomeView from "../views/HomeView.vue";

const routes = [
  {
    path: "/",
    name: "Vue3初步学习",
    meta: {
      title: "测试",
    },
    children: [
      {
        path: "/home",
        name: "home",
        component: HomeView,
        meta: {
          title: "主页3",
        },
      },
    ],
  },
  {
    path: "/about",
    name: "about",
    meta: {
      title: "第一阶段",
    },

    component: () => import("../views/AboutView.vue"),
  },
  {
    path: "",
    redirect: "/home",
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});

interface RouteWithoutComponent {
  children?: RouteWithoutComponent[];
  label: string;
  name: string;
}

function removeComponentsFromRoutes(
  routes: RouteRecordRaw[]
): RouteWithoutComponent[] {
  return routes.map((route) => {
    const { children, meta } = route;
    const processedRoute: RouteWithoutComponent = {
      label: (meta?.title as string) ?? "未定义名字",
      name: route.name as string,
    };
    if (children && children.length > 0) {
      processedRoute.children = removeComponentsFromRoutes(children);
    }
    return processedRoute;
  });
}
const list = removeComponentsFromRoutes(routes);
export const routerMessages = list.slice(0, list.length - 1);

export default router;
